---
description: Use Postgres triggers with Hasura
keywords:
  - hasura
  - docs
  - postgres
  - triggers
sidebar_position: 5
sidebar_label: Triggers
---

# Postgres Triggers

## Introduction

[Postgres triggers](https://www.postgresql.org/docs/current/sql-createtrigger.html) are used to invoke previously
defined Postgres functions _before_ or _after_ a specific database event (e.g. `INSERT`) occurs.

:::info Note

For more information on Postgres triggers, please refer to the
[Postgres documentation](https://www.postgresql.org/docs/current/sql-createtrigger.html). We also have a Learn Tutorial
focusing on PostgresSQL. [Information on triggers](https://hasura.io/learn/database/postgresql/triggers/) can be found
here.

:::

## Examples

**Trigger a Postgres function before an article is inserted or updated:**

Let's say we want to check if an author is active before a corresponding article can be inserted or updated. We can do
so with the following Postgres function:

```plsql
CREATE FUNCTION check_author_active()
    RETURNS trigger AS $BODY$
    DECLARE active_author BOOLEAN;
    BEGIN
    SELECT author.is_active INTO active_author FROM "authors" author WHERE author.id = NEW."author_id";
    IF active_author != TRUE THEN
        RAISE EXCEPTION 'Author must be active';
    END IF;
    RETURN NEW;
    END;
    $BODY$ LANGUAGE plpgsql;
```

Now we want to have this function executed whenever a new article is about to be inserted or updated. We can create a
Postgres trigger as follows:

```plsql
CREATE TRIGGER insert_article BEFORE INSERT OR UPDATE ON "articles" FOR EACH ROW EXECUTE PROCEDURE check_author_active();
```

If someone now tries to insert an article for an author that is not active, the following error will be thrown:

```plsql
unexpected : Author must be active
```

**Refresh a materialized view when an author gets inserted:**

Let's say we want to refresh a materialized view whenever a new author is inserted.

The following Postgres function refreshes a materialized view:

```plsql
CREATE FUNCTION refresh_materialized_view()
  RETURNS trigger AS $BODY$
  BEGIN
  REFRESH MATERIALIZED VIEW popular_active_authors;
  RETURN NULL;
  END;
  $BODY$ LANGUAGE plpgsql;
```

Now, to make sure this function gets called whenever a new author is inserted, we can create the following Postgres
trigger:

```plsql
CREATE TRIGGER update_materialized_view AFTER INSERT ON "authors" FOR EACH ROW EXECUTE PROCEDURE refresh_materialized_view();
```

## Postgres triggers & Hasura

Postgres triggers can be used to perform business logic such as data validation and can be added
[as described here](/schema/postgres/data-validations.mdx#pg-data-validations-pg-triggers).

:::info Note

Hasura also has [Event Triggers](/event-triggers/overview.mdx) that can be used to invoke external HTTP APIs for executing
custom business logic on database events.

:::
